function OrgsController() {

    this.orgsTreeController = new OrgsTreeController();
    this.orgController = new OrgController();

    var orgsControllerThis = this;

    $(document).bind('orgsDisable', function(e) {
        $(".orgvault_orgs_button").attr('disabled', 'disabled');
    });
    $(document).bind('orgsEnable', function(e) {
        $(".orgvault_orgs_button").removeAttr('disabled');
    });

    $("#pageHelp").click(function() {
        console.log("orgsController pageHelp click");
        if($("#orgs_container_id").is(":visible")){
            console.log("orgs visible");
            var message = "Organizations help..."
                + "\nNavigate the hierarchy by expanding nodes in the tree."
                + "\nClick on a node to see the details."
                + "\nDrag and drop to move an organization to a new place in the structure."
                + "\nCheck to mark organizations for deletion."
                + "\nChecking a parent checks all of its children."
                + "\nHover for hints."
                + "\nRight mouse context menu for edit / delete / add child / add sibling."
            alert(message);
        }else{
            console.log("orgs not visible");
        }
    });
    

    if (typeof OrgsController.prototype.getById == "undefined") {

        // Activates the requested node.
        // If the list has a stale version, then the orgs list is
        // repopulated before the activation.
        // May expand intermediate notes to get to the ultimate child.
        OrgsController.prototype.getById = function(id, version, parentKeys) {
            var dfd = $.Deferred(); // this one gets returned
            // Function is invoked as an object method, so this points to the
            // orgsController instance.
            if ((typeof this.orgsTreeController != "undefined") && (version == this.orgsTreeController.treeVersion)) {
                // have a good tree, not stale
                var targetNode = this.orgsTreeController.getNode(id);
                if (!targetNode.isActive()) {
                    targetNode.activate();
                }
                var promise = this.orgController.get(id);
                $.when(promise).then(function() {
                    dfd.resolve();
                });
            } else {
                // Have a stale version of the tree. Open the modal dialog box
                $("#dialog-modal").dialog("open");
                // Get a fresh list of orgs from the server.
                // Redisplay the orgs tree.
                // Redisplay the detail pane.
                $.when(this.get(id)).then(function() {
                    $("#dialog-modal").dialog("close");
                    var targetNode = orgsControllerThis.orgsTreeController.activate(id, parentKeys);
                    if (targetNode != null) {
                        $.when(orgsControllerThis.orgController.get(id)).then(function() {
                            dfd.resolve();
                        });
                    } else {
                        dfd.resolve();
                    }
                });
            }
            return dfd.promise();
        }
        // gets the list, activates the first node
        OrgsController.prototype.getWithNoId = function() {
            $("#dialog-modal").dialog("open");
            var dfd = $.Deferred();
            $.when(this.get()).then(function() {
                $("#dialog-modal").dialog("close");
                var firstNode = orgsControllerThis.orgsTreeController.activateFirst();
                if (firstNode == null) {
                    dfd.resolve();
                } else {
                    $.when(orgsControllerThis.orgController.get(firstNode.data.key)).then(function() {
                        dfd.resolve();
                    });
                }
            });

            return dfd.promise();
        }

        // Gets the collection of orgs.
        // Causes the tree to be loaded from that collection.
        OrgsController.prototype.get = function(id) {
            var dfd = $.Deferred(); // this one gets returned

            var orgs = {};
            var options = {
                'success' : function(data) {
                    // is a callback function, so "this" is set by the caller.
                    orgs = data.orgs;
                }
            };
            var jqXHR_get = new AjaxHelper(options).getJSON("/orgGetList.app");

            var jqXHR_getTemplate = {};
            if (typeof $.template["compiled_orgs_template"] == "undefined") {
                jqXHR_getTemplate = $.get('/template/orgs.html', function(templateData) {
                    $.template("compiled_orgs_template", templateData);
                });
            }
            var deferred = $.when(jqXHR_get, jqXHR_getTemplate).then(function() {
                orgsControllerThis.loadForm(orgs);
                // only want if no id is about to be requested
                if (typeof id == "undefined") {
                    // does not cause an ajax call
                    orgsControllerThis.orgsTreeController.activateFirst();
                }
                dfd.resolve();
            });
            return dfd.promise();
        }

        OrgsController.prototype.loadForm = function(orgs) {
            $(".fap_container_class").hide().empty();
            $.tmpl("compiled_orgs_template", orgsControllerThis).appendTo("#orgs_container_id");

            $('#org_delete_button_id').click(function() {
                orgsControllerThis.orgsTreeController.deleteClickHandler();
            });

            $('#org_uncheck_all_button_id').click(function() {
                orgsControllerThis.orgsTreeController.uncheckAll();
            });

            $('#org_add_sibling_button_id').click(function() {
                orgsControllerThis.orgsTreeController.addSibling();
            });

            $('#org_add_child_button_id').click(function() {
                orgsControllerThis.orgsTreeController.addChild();
            });

            $('#deletion_checkbox_id').click(function() {
                if (this.checked) {
                    $("input[name='org_id_to_delete_name']").attr('checked', true);
                } else {
                    $("input[name='org_id_to_delete_name']").attr('checked', false);
                }
            });
            $("#orgs_container_id").show();
            // needs to be a method... cause the tree to be loaded
            this.orgsTreeController.loadTree(orgs);
        }
    }
}
